package classProjectNameScore;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Question {
	ArrayList<Score> list = new ArrayList<Score>(); 
	
	public Question() throws IOException{
		FileReader fr = new FileReader("scores.txt");
		BufferedReader br = new BufferedReader(fr);
		String str = "";
		while((str = br.readLine()) != null){
			String[] contents = str.split(":");
			Score score = new Score(contents[0],contents[1],contents[2],Integer.parseInt(contents[0]));
			list.add(score);
		}
	}
	
	public Score highScore(){
		Score temp = new Score();
		for (Score score : list) {
			if(temp.getScore() < score.getScore()){
				temp = score;
			}
		}
		return temp;
	}
	
	public Map<String,Map<String,Integer>> classNumHighScore(){
		Map<String,Map<String, Integer>> highScore = new HashMap<String,Map<String, Integer>>();
		Map<String, List<Score>> byClass = list.stream().collect(Collectors.groupingBy((t)->((Score)t).getClassName()));
		byClass.forEach((className,scores1)->{
			Map<String, Integer> student = new HashMap<String,Integer>();
			Map<String, Integer> temp = new HashMap<String,Integer>();
			Map<String,List<Score>> byStudentName = scores1.stream().collect(Collectors.groupingBy((t)->t.getStudentName()));
			byStudentName.forEach((studentName,scores2)->{
				int total = (int)scores2.stream().collect(Collectors.summarizingInt((t)->((Score)t).getScore())).getSum();
				temp.put(studentName, total);
			});
			temp.values().stream().max((o1,o2)->((Integer)o1).compareTo((Integer)o2));
		});
		return highScore;
	}
}
